
package com.csc440.team2.activity;

import java.util.List;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;


import com.csc440.team2.SmilConstants;
import com.csc440.team2.communication.CommunicationManager;
import com.csc440.team2.database.entities.FolderType;
import com.csc440.team2.database.entities.Message;
import com.csc440.team2.smil.SMILComponent;
import com.csc440.team2.smil.SMILMessage;
import com.csc440.team2.smil.SMILReadException;


public class ComponentsTest extends Activity
{
    private SMILMessage message;


    @Override
    public void onCreate(Bundle savedIntanceState)
    {
        super.onCreate(savedIntanceState);
        setContentView(R.layout.components_test);

        final Bundle bundle = getIntent().getExtras();

        if (bundle != null)
        {
            message = (SMILMessage) bundle.getSerializable(SmilConstants.MESSAGE);

            Button button = (Button) findViewById(R.id.AddItems);
            button.setOnClickListener(new OnClickListener()
            {

                public void onClick(View v)
                {
                    Intent in = new Intent(ComponentsTest.this, AddComponents.class);
                    in.putExtra(SmilConstants.MESSAGE, message);
                    ComponentsTest.this.startActivityForResult(in, 1);
                }

            });

            Button sendButton = (Button) findViewById(R.id.Send);
            sendButton.setOnClickListener(new OnClickListener()
            {

                public void onClick(View v)
                {
                    saveMessage();
                    sendMessage();
                    finish();
                }

            });
            
            Button previewButton = (Button) findViewById(R.id.Preview);
            previewButton.setOnClickListener(new OnClickListener()
            {

                public void onClick(View v)
                {
                	Intent myIntent = new Intent(v.getContext(), SMILPlayer.class);
                    myIntent.putExtra(SmilConstants.MESSAGE, message);
                    startActivity(myIntent);
                }

            });

            Button saveButton = (Button) findViewById(R.id.Save);
            saveButton.setOnClickListener(new OnClickListener()
            {

                public void onClick(View v)
                {
                	draftMessage(message, bundle);
                    finish();
                }

            });
            
            ListView lv = (ListView) findViewById(R.id.ComponentsList);

            List<SMILComponent> list = getComponents(message);
            if (list.size() == 0)
                lv.setAdapter(new ArrayAdapter<String>(this, R.layout.components, new String[] {
                    "No components to display"
                }));
            else
                lv.setAdapter(new ArrayAdapter<SMILComponent>(this, R.layout.components, getComponents(message)));

            lv.setTextFilterEnabled(true);

            lv.setOnItemClickListener(new OnItemClickListener()
            {
                public void onItemClick(AdapterView<?> parent, View clickedView, int position, long clickedItemId)
                {
                    // have to set the new view
                    if (position == SmilConstants.SELECT_ADD_COMPONENT)
                    {

                        Intent in = new Intent(ComponentsTest.this, AddComponents.class);
                        ComponentsTest.this.startActivity(in);
                    }
                    else if (position == SmilConstants.SELECT_REMOVE_COMPONENT)
                    {
                        Intent in = new Intent(ComponentsTest.this, RemoveComponents.class);
                        ComponentsTest.this.startActivity(in);
                    }
                }
            });
        }
    }


    private void saveMessage()
    {
        try
        {
            Message dbMessage;

            dbMessage = new Message(message.getXml(), FolderType.SENT, true, "");

            if (dbMessage.save(this))
                Log.i("Save", "Message Saved");
            else
                Log.i("Save", "Failed to save message");

        }
        catch (SMILReadException e)
        {
            Log.e("Save", "Could not save message", e);
        }
    }
    
    private void draftMessage(SMILMessage message, Bundle bundle)
    {
    	SMILMessage tempMessage = (SMILMessage) bundle.getSerializable(SmilConstants.MESSAGE);
        try
        {
        	if(tempMessage != message){
        		Message dbMessage;

                dbMessage = new Message(message.getXml(), FolderType.DRAFT, true, "");

                if (dbMessage.save(this))
                    Log.i("Save", "Message Saved");
                else
                    Log.i("Save", "Failed to save message");
        	}
        }
        catch (SMILReadException e)
        {
            Log.e("Save", "Could not save message", e);
        }
    }    


    private void sendMessage()
    {
        try
        {
            EditText toBox = (EditText) findViewById(R.id.ToBox);
            String reciever = toBox.getText().toString();
            CommunicationManager.sendSMILMessage(message.getXml(), reciever);
        }
        catch (SMILReadException e)
        {
            Log.e("Send", "Could not send message", e);
        }
    }


    private List<SMILComponent> getComponents(SMILMessage message)
    {
        return message.getComponentList();
    }


    @Override
    protected void onSaveInstanceState(Bundle bundle)
    {
        bundle.putSerializable(SmilConstants.MESSAGE, message);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        ListView lv = (ListView) findViewById(R.id.ComponentsList);
        SMILMessage returnedMessage = (SMILMessage) data.getExtras().get(SmilConstants.MESSAGE);
        message = returnedMessage;
        System.out.println("activity returned");
        lv.setAdapter(new ArrayAdapter<SMILComponent>(this, R.layout.components, getComponents(message)));
    }
}
